package com.example.shangjinlieren.utils;

import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import com.zzrv5.mylibrary.ZZRResponse;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Image;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;


public class HmOSImageLoader {
    private final static HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0, "HmOSImageLoader");
    Image image;
    String url;
    int defImage;
    AbilitySlice abilitySlice;

    public HmOSImageLoader(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;
    }

    public static HmOSImageLoader with(AbilitySlice abilitySlice) {
        return new HmOSImageLoader(abilitySlice);
    }

    public HmOSImageLoader load(String url) {
        this.url = url;
        return this;
    }

    public HmOSImageLoader def(int defImage) {
        this.defImage = defImage;
        return this;
    }

    public void into(Image image) {
        this.image = image;
        start();
    }

    private void start() {
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                //http访问出错了，此部分内容在主线程中工作;
                //可以更新UI等操作,请不要执行阻塞操作。
                System.out.println("errorMessage"+errorMessage);
            }

            @Override
            public String onParseResponse(ZZRResponse response) {
                //创建图片源
                ImageSource imageSource = ImageSource.create(response.inputStream, null);
                //根据图片源创建位图
                PixelMap pixelMap = imageSource.createPixelmap(null);
                abilitySlice.getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        //展示到组件上
                        System.out.println("挂载了");
                        image.setPixelMap(pixelMap);
                        pixelMap.release();
                    }
                });
                return super.onParseResponse(response);
            }

            @Override
            public void onResponse(String response) {

            }
        });
//        if (defImage != 0)
//            image.setPixelMap(defImage);
//        Request request = new Request.Builder().url(url).get().build();
//        new Thread(() -> {
//            OkHttpClient okHttpClient = new OkHttpClient();
//            try {
//                //异步网络请求
//                Response execute = okHttpClient.newCall(request).execute();
//                //获取流
//                InputStream inputStream = execute.body().byteStream();
//                //利用鸿蒙api将流解码为图片源
//                ImageSource imageSource = ImageSource.create(inputStream, null);
//                //根据图片源创建位图
//                PixelMap pixelMap = imageSource.createPixelmap(null);
//
////                abilitySlice.getUITaskDispatcher().asyncDispatch(() -> );
//                abilitySlice.getUITaskDispatcher().asyncDispatch(new Runnable() {
//                    @Override
//                    public void run() {
//                        //展示到组件上
//                        System.out.println("挂载了");
//                        image.setPixelMap(pixelMap);
//                        pixelMap.release();
//                    }
//                });
//                //释放位图
//
//            } catch (IOException e) {
//                HiLog.error(LABEL_LOG, " ----- " + e.getMessage());
//                e.printStackTrace();
//            }
//        }).start();
    }
}
